package com.gxa.modules.pc.fee.controller;

import com.gxa.common.utils.FilePortUtil;
import com.gxa.common.utils.PageUtils;
import com.gxa.common.utils.Result;
import com.gxa.modules.model.dto.DetailInfoDto;
import com.gxa.modules.model.dto.RechargeDetailRecordDto;
import com.gxa.modules.model.dto.RefundSearchDto;
import com.gxa.modules.model.entity.fee.Patient;
import com.gxa.modules.model.entity.fee.RechargeOrderDetail;
import com.gxa.modules.pc.fee.service.InpatientRechargeRecordsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 住院充值记录表 前端控制器
 * </p>
 *
 * @author gxa
 * @since 2022-11-12 05:31:54
 */
@Api(tags = "住院充值记录表")
@RestController
@RequestMapping("/inpatient-recharge-records")
public class InpatientRechargeRecordsController {
    @Autowired
    private InpatientRechargeRecordsService inpatientRechargeRecordsService;

    @PostMapping("/list")
    @ApiOperation("住院充值记录表分页查询")
    @ApiResponses({
            @ApiResponse(code = 0,message = "success", response = RechargeDetailRecordDto.class)
    })
    public Result list(@RequestBody RefundSearchDto refundSearchDto){
        try {
            PageUtils pageUtils = this.inpatientRechargeRecordsService.queryListSearch(refundSearchDto);
            return new Result().ok(pageUtils);
        }catch (Exception e){
            e.printStackTrace();
            return new Result().error("查询失败");
        }
    }

    @PostMapping("/list/{patientId}/{inInfoId}/{rechargeOrderId}")
    @ApiOperation("住院充值记录表详细信息查询")
    @ApiResponses({
            @ApiResponse(code = 0,message = "success", response = Patient.class),
            @ApiResponse(code = 0,message = "success", response = DetailInfoDto.class),
            @ApiResponse(code = 0,message = "success", response = RechargeOrderDetail.class)
    })
    public Result listInfo(@RequestParam("patientId") Integer patientId,@RequestParam("inInfoId") Integer inInfoId,@RequestParam("rechargeOrderId") Integer rechargeOrderId){
        try {
            Map<String, Object> map = this.inpatientRechargeRecordsService.queryInfo(patientId, inInfoId, rechargeOrderId);
            return new Result().ok(map);
        }catch (Exception e){
            e.printStackTrace();
            return new Result().error("查询失败");
        }
    }

    @GetMapping(value = "/fileport")
    @ApiOperation("导出住院充值记录表")
    public Result filePort(HttpServletResponse response) {
       try{
           //导出的表名
           String title = "导出住院充值记录";
           //表中第一行表头字段
           String[] headers = {"主键id", "操作时间","充值订单的id","患者的id","住院信息id","订单总额","0:微信支付1：住院预交金抵扣","住院余额","0：未支付，1：已支付","订单编号（后台自动生成）","0:微信小程序","0:住院充值，1：门诊充值","患者的姓名","就诊卡号（后台自动为就诊人生成卡号）","余额","住院号"};
           //实际数据结果集
           List<RechargeDetailRecordDto> rechargeDetailRecordDtoList = this.inpatientRechargeRecordsService.queryList();
           //具体需要写入excel需要哪些字段，这些字段取自RechargeDetailRecordDto类，也就是上面的实际数据结果集的泛型
           List<String> listColumn = Arrays.asList("id", "createTime", "rechargeOrderId", "patientId","inInfoId","orderTotal","payMethod","balance","status","orderNum", "orderFrom","businessType","name","visitNum","money","inpatientNum");
           try {
               FilePortUtil.exportExcel(response, title, headers, rechargeDetailRecordDtoList, listColumn);
           } catch (Exception e) {
               e.printStackTrace();
               return new Result().error("导出失败");
           }
           return new Result().ok("导出成功");
       }catch (Exception e){
           e.printStackTrace();
           return new Result().error("导出失败");
       }

    }


}
